# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
import django.utils.timezone as timezone
from django.contrib import auth
from  django.contrib.auth.models import  AbstractUser

from  django.contrib.auth.models import  Permission,Group,GroupManager,PermissionManager,PermissionsMixin
from  django.core.validators import RegexValidator

from  .aes_decryptor import  Prpcrypt


# Create your models here.
#主机表
class HostInfo(models.Model):
	ip = models.CharField(max_length=20, verbose_name=u'IP地址')
	kernel = models.CharField(max_length=30, verbose_name=u'操作系统')
	num_cpus = models.CharField(max_length=20, verbose_name=u'cpu核数')
	mem_total = models.CharField(max_length=20, verbose_name=u'内存信息')
	hostname = models.CharField(max_length=20, verbose_name=u'主机名')
	sn = models.CharField(max_length=256, verbose_name=u'SN',default=True)
	host_remove_port = models.CharField(max_length=64, default=True)
	host_user = models.CharField(max_length=128,default=True)
	host_passwd = models.CharField(max_length=256,default=True)
	def __str__(self):
		return u'%s' % (self.ip)

class Host(models.Model):
    """服务器基本信息"""
    host_ip = models.CharField(max_length=64,unique=True)
    host_remove_port = models.CharField(max_length=64,null=True)
    host_user = models.CharField(max_length=128)
    host_passwd = models.CharField(max_length=256)
    host_type = models.CharField(max_length=64)

    creat_time = models.DateTimeField(auto_now_add=True)
    def __str__(self):
        return self.host_ip

class HostPermission(models.Model):
    name = models.CharField("权限名称", max_length=64)
    url = models.CharField('URL名称', max_length=255)
    chioces = ((1, 'GET'), (2, 'POST'))
    per_method = models.SmallIntegerField('请求方法', choices=chioces, default=1)
    argument_list = models.CharField('参数列表', max_length=255, help_text='多个参数之间用英文半角逗号隔开', blank=True, null=True)
    describe = models.CharField('描述', max_length=255)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '权限URL表'
        verbose_name_plural = verbose_name
        #权限信息，这里定义的权限的名字，后面是描述信息，描述信息是在django admin中显示权限用的
        permissions = (
            ('views_host_list', '查看主机列表'),
            ('views_host_info', '查看主机信息'),
        )





class SaltHost(models.Model):
	hostname = models.CharField(
		max_length=80,
		unique=True,
		verbose_name=u'主机名称')
	# salt主机存活状态
	alive = models.BooleanField(default=False, verbose_name=u'连通状态')
	# 上次检测时间
	alive_time_last = models.DateTimeField(auto_now=True)
	# 当前检测时间
	alive_time_now = models.DateTimeField(auto_now=True)
	status = models.BooleanField(default=False, verbose_name=u'是否加入salt管理')
	# user_group = models.ManyToManyField(UserGroup, related_name='host_usergroup_set', verbose_name=u'所属用户组')
	# department = models.ManyToManyField(Department, related_name='host_department_set', verbose_name=u'所属部门')

	def __str__(self):
		return self.hostname
	class Meta:
		default_permissions = ()
		permissions = (
			("view_deploy", u"查看主机部署"),
			("edit_deploy", u"管理主机部署"),
			("edit_salthost", u"管理Salt主机")
		)
		verbose_name = u'Salt主机授权'
		verbose_name_plural = u'Salt主机授权管理'

# class users(AbstractUser):
# 	display = models.CharField('显示的中文名', max_length=50)
# 	role = models.CharField('角色', max_length=20, choices=(('工程师', '工程师'), ('审核人', '审核人')), default='工程师')
# 	def __str__(self):
# 		return self.username
# 	class Meta:
# 		verbose_name = u'用户配置'
# 		verbose_name_plural = u'用户配置'


#各个线上主库地址。
class master_config(models.Model):
    cluster_name = models.CharField('集群名称', max_length=50,help_text='可以用英文名字')
    master_host = models.CharField('主库地址', max_length=200)
    master_port = models.IntegerField('主库端口', default=3306)
    master_user = models.CharField('登录主库的用户名', max_length=100)
    master_password = models.CharField('登录主库的密码', max_length=300)
    create_time = models.DateTimeField('创建时间', auto_now_add=True)
    update_time = models.DateTimeField('更新时间', auto_now=True)


    def __str__(self):
        return self.cluster_name
    class Meta:
        verbose_name = u'主库地址'
        verbose_name_plural = u'主库地址'

    def save(self, *args, **kwargs):
        pc = Prpcrypt() #初始化
        self.master_password = pc.encrypt(self.master_password)
        super(master_config, self).save(*args, **kwargs)

#用户表
class UserInfo(models.Model):
	username = models.CharField(max_length=10,null=None,verbose_name='用户名')
	password = models.CharField(max_length=10,verbose_name='密码')
	user_group = models.ForeignKey("UserGroup",to_field="uid",default=1)
	user_type_choice = (
		(0,u'普通用户'),
		(1,u'高级用户'),
		(3,u'超级用户')
	)
	mobile = models.CharField(max_length=20,null=None,verbose_name='手机号',default=True)
	email = models.CharField(max_length=20,null=None,verbose_name='邮箱',default=True)

	user_type_id = models.IntegerField(choices=user_type_choice,default=1,verbose_name='用户类型')
	create_time = models.DateTimeField(null=True,auto_now_add=True)
	up_time = models.DateTimeField(null=True,default=timezone.now)
	# add_date = models.DateTimeField('保存日期', default=timezone.now)
	# mod_date = models.DateTimeField('最后修改日期', auto_now=True)
	def __unicode__(self):
		return u'%s-%s' % (self.username,self.user_group)
	class Meta:
		ordering = ['-id']
		permissions = (
			('virew_user',u'查看用户权限'),
			('edit_user',u'管理用户'),
		)
#用户组
class UserGroup(models.Model):
	uid = models.AutoField(primary_key=True)
	group_name = models.CharField(max_length=20,unique=True)
	comment = models.TextField(blank=True, null=True, verbose_name=u'备注')
	ctime = models.DateTimeField(auto_now_add=True,null=True)
	uptime = models.DateTimeField(auto_now=True,null=True)

	def  __unicode__(self):
		return self.group_name

'''
文件上传
'''
class UploadFileForm(models.Model):
	username = models.CharField('用户名', max_length=30,default=True)
	# 用户名
	headImg = models.FileField('文件', upload_to='./uploads/',default=True)  # 文件名

	def __str__(self):
		return self.username

	class Meta:
		ordering = ['username']  # 排序风格username
class UserGroup02(Group):
	group_name = models.CharField(max_length=80, unique=True, verbose_name=u'用户组')
	comment = models.TextField(blank=True, null=True, verbose_name=u'备注')
	def clean(self):
		self.name = self.group_name
	def __unicode__(self):
		return self.group_name
	class Meta:
		default_permissions = ()
		verbose_name = u'用户组'
		verbose_name_plural = u'用户组管理'

class Dashboard_status(models.Model):
    up = models.IntegerField(null=True, blank=True,verbose_name=u'minion的status是up的数量')
    down = models.IntegerField(null=True, blank=True,verbose_name=u'minion的status是down的数量')
    accepted = models.IntegerField(null=True, blank=True,verbose_name=u'accepted的数量')
    unaccepted = models.IntegerField(null=True, blank=True,verbose_name=u'unaccepted的数量')
    rejected = models.IntegerField(null=True, blank=True,verbose_name=u'rejected的数量')
    def __unicode__(self):
        return u'%s %s %s %s %s' % (self.up, self.down, self.accepted, self.unaccepted, self.rejected)
	class Meta:
		db_table = "dashboard_status"


class Product(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    name = models.CharField(max_length=100, default='')
    describe = models.CharField(max_length=500, default='')
    price = models.FloatField()
    isDelete = models.BooleanField(default=False)

    class Meta:
        ordering = ('created',)


class Student(models.Model):
	name = models.CharField('姓名', max_length=64)
	age = models.SmallIntegerField('年龄')
	choices = (
		(1, '男'),
		(2, '女'),
		(3, '未知')
	)
	sex = models.SmallIntegerField('性别', choices=choices)
	def __unicode__(self):
		return self.name


class UserLog(models.Model):
	user_name = models.CharField('操作人',max_length=32)
	url_title = models.CharField('url',max_length=32)
	file_name = models.CharField(max_length=32)
	up_path = models.CharField(max_length=32)
	create_time = models.DateTimeField(auto_now=True)



class Redisconf(models.Model):
	TYPE_CHOICES = (
		(0, u'单机'),
		(1, u'cluster')
	)
	name = models.CharField(max_length=10, verbose_name=u"名称")
	host = models.CharField(max_length=15, verbose_name=u"IP地址")
	port = models.IntegerField(default=6379, verbose_name=u"端口")
	password = models.CharField(null=True, blank=True, max_length=30, verbose_name=u"密码")
	database = models.IntegerField(default=16, verbose_name=u"db数")
	type = models.IntegerField(default=0, choices=TYPE_CHOICES, verbose_name=u"类型")

	class Meta:
		verbose_name = "redis配置"
		verbose_name_plural = verbose_name
		db_table = 'redis_config'


from  DjangoUeditor.models import UEditorField


"""

1、将更改记录为迁移文件
	python manage.py makemigrations blog
2、查看迁移行为将会执行哪些SQL语句
	python manage.py sqlmigrate blog 0001
3、检查你的项目中的模型是否存在问题
	python manage.py check
4、运行migrate以在你的数据库中创建模型所对应的表
	python manage.py migrate
5、运行解析器
	python manage.py shell
	primary-key查询，Question.objects.get(pk=1)相等于Question.objects.get(id=1)
"""


